<?php

namespace Home\Controller;

/**
 * 游戏管理
 *
 * @author Administrator
 *        
 */
class GameController extends BaseController {
	/**
	 * 获取大厅信息
	 */
	public function mb_GetLobbyInfo() {
		$data = $this->mb_OnMessage ();
		$lobbyInfo = M ( 'Lobbyinfo' );
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $lobbyInfo->find () ) );
	}
	/**
	 * 重设大厅信息
	 */
	public function mb_ResetLobbyInfo() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$status = $data->status;
		$regist = $data->regist;
		$login = $data->login;
		$phone = $data->phone;
		
		$bulletin = $data->bulletin;
		$broadcast = $data->broadcast;
		$recharge = $data->recharge;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$lobbyInfo = M ( 'Lobbyinfo' );
		$save = array (
				'id' => 1,
				'status' => $status,
				'sw_regist' => $regist,
				'sw_login' => $login,
				'sw_phone' => $phone,
				'bulletin' => $bulletin,
				'broadcast' => $broadcast,
				'recharge' => $recharge 
		);
		
		$lobbyInfo->where ( "id='1'" )->save ( $save );
		\WMHandler::mb_GetInstance ()->mb_UpdateLobbyInfo ( $status, $regist, $login, $phone, $bulletin, $broadcast, $recharge );
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 重设部分信息
	 */
	public function mb_ResetInfo() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$type = $data->type; // 0：公告，1：广播，2：充值
		$value = $data->value;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$lobbyInfo = M ( 'Lobbyinfo' );
		if ($type == 0) {
			// 公告
			$key = 'bulletin';
		} else if ($type == 1) {
			// 广播
			$key = 'broadcast';
		} else if ($type == 2) {
			// 充值
			$key = 'recharge';
		}
		$save = array (
				"$key" => $value 
		);
		$lobbyInfo->where ( "id='1'" )->save ( $save );
		\WMHandler::mb_GetInstance ()->mb_UpdateLobbyMessage ( $key, $value );
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	public function mb_GetGameConfig() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$gameId = $data->gameid;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		
		if ($gameId == '12') {
			$config = M ( 'Config', '', 'DB_JINSHAYINSHA' );
			$configData = $config->find ();
			$gameData ['maxbet'] = $configData ['maxbet'];
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $gameData ) );
	}
	public function mb_SetGameConfig() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$gameId = $data->gameid;
		$status = $data->status;
		$level = $data->level;
		$coinValue = $data->coinvalue;
		$maxbet = $data->maxbet;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameInfo->save ( array (
				'id' => $gameId,
				'coinvalue' => $coinValue,
				'level' => $level,
				'status' => $status 
		) );
		
		$lobbyAppInfo = M ( 'Appinfo', '', 'DB_LOBBY' );
		$lobbyAppInfo->save ( array (
				'id' => $gameId,
				'status' => $status 
		) );
		
		if ($gameId == 12) {
			$config = M ( 'Config', '', 'DB_JINSHAYINSHA' );
			$config->where ( '1' )->save ( array (
					'maxbet' => $maxbet 
			) );
			\WMHandler::mb_GetInstance ()->mb_UpdateGameLevel ( $gameId, $level );
		}
		
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		$gameUrl = $gameData ['url'];
		
		$reqObj = new \Req_sfsGame ();
		$reqObj->SetLevel ( $gameUrl, $level );
		
		\WMHandler::mb_GetInstance ()->mb_UpdateGameStatus ( $gameId, $status );
		\WMHandler::mb_GetInstance ()->mb_UpdateGameCoinValue ( $gameId, $coinValue );
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $gameData ) );
	}
	public function mb_GetGameLog() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$gameId = $data->gameid;
		$id = $data->id;
		$name = $data->name;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameLog = M ( 'Gamelog' );
		if ($id != '') {
			$logDatas = $gameLog->where ( "gameid='$gameId' AND userid='$id'" )->order ( 'id desc' )->limit ( 5000 )->select ();
		} else if ($name != '') {
			$logDatas = $gameLog->where ( "gameid='$gameId' AND username='$name'" )->order ( 'id desc' )->limit ( 5000 )->select ();
		} else if ($id == '' && $name == '') {
			$logDatas = $gameLog->where ( "gameid='$gameId'" )->order ( 'id desc' )->limit ( 5000 )->select ();
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $logDatas ) );
	}
	public function mb_GetGameStatus() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$gameId = $data->gameid;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $gameData ) );
	}
	public function mb_SetGameStatus() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$gameId = $data->gameid;
		$status = $data->status;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameInfo->save ( array (
				'id' => $gameId,
				'status' => $status
		) );
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		
		$lobbyAppInfo = M ( 'Appinfo', '', 'DB_LOBBY' );
		$lobbyAppInfo->save ( array (
				'id' => $gameId,
				'status' => $status
		) );
		\WMHandler::mb_GetInstance ()->mb_UpdateGameStatus ( $gameId, $status );
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $gameData ) );
	}
}